package adapter;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.example.localvideo.R;

import bean.VideoBean;

//视频播放列表的数据适配器
public class VideoListAdapter extends CursorAdapter {

    private View mView;

    public VideoListAdapter(Context context, Cursor c) {
        super(context, c);
    }

    //加载listview条目布局文件
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        mView = View.inflate(context, R.layout.videoitem,null);
        mView.setTag(new ViewHolder());
        return mView;
    }

    //给条目布局里的控件设置数据
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();

        VideoBean bean = VideoBean.getInstanceFromCursor(cursor);

        holder.tvTitle.setText(bean.getTitle());
        //格式化一下时间和大小，现在这样太难看了
        String size = Formatter.formatFileSize(context,bean.getSize());
        holder.tvSize.setText(String.valueOf(size));
        //时间
        final int HOUR = 60*60*1000;
        final int MIN = 60*1000;
        final int SEC = 1000;
        int time = bean.getTime();
        int hour = time / HOUR;
        int min = time % HOUR / MIN;
        int sec = time %MIN /SEC;
        //判断是否超过一小时
        String showTime;
        if (hour >=1){//00:00:00
            showTime =  String.format("%02d:%02d:%02d",hour,min,sec);
        }else{//00:00
            showTime =  String.format("%02d:%02d",min,sec);
        }
        holder.tvTime.setText(showTime);
    }

    class ViewHolder{
        private TextView tvTitle,tvTime,tvSize;

        public ViewHolder() {
            tvTitle = (TextView)mView.<TextView>findViewById(R.id.tv_title);
            tvTime = (TextView)mView.findViewById(R.id.tv_time);
            tvSize = (TextView)mView.findViewById(R.id.tv_size);
        }
    }
}
